﻿package net.wangit.jdbc.util;

import java.sql.SQLException;
import java.sql.SQLWarning;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class JDBCExceptionReporter {

	private static final Log log = LogFactory
			.getLog(net.wangit.jdbc.util.JDBCExceptionReporter.class);

	private JDBCExceptionReporter() {
	}

	public static void logWarnings(SQLWarning warning) {
		if (log.isWarnEnabled()) {
			if (log.isDebugEnabled() && warning != null)
				log.debug("SQL Warning", warning);
			for (; warning != null; warning = warning.getNextWarning()) {
				log.warn((new StringBuffer(30)).append("SQL Warning: ").append(
						warning.getErrorCode()).append(", SQLState: ").append(
						warning.getSQLState()).toString());
				log.warn(warning.getMessage());
			}

		}
	}

	public static void logExceptions(SQLException ex) {
		if (log.isErrorEnabled()) {
			if (log.isDebugEnabled())
				log.debug("SQL Exception", ex);
			for (; ex != null; ex = ex.getNextException()) {
				log.warn((new StringBuffer(30)).append("SQL Error: ").append(
						ex.getErrorCode()).append(", SQLState: ").append(
						ex.getSQLState()).toString());
				log.error(ex.getMessage());
			}

		}
	}

}
